<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="ID"
      >
      </el-table-column>
      <el-table-column
        prop="courses"
        header-align="center"
        align="center"
        label="课程号"
      >
      </el-table-column>
      <el-table-column
        prop="grade"
        header-align="center"
        align="center"
        label="年级"
      >
      </el-table-column>
      <el-table-column
        prop="majorId"
        header-align="center"
        align="center"
        label="专业ID"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间"
      >
      </el-table-column>
      <el-table-column
        prop="isDelete"
        header-align="center"
        align="center"
        label="是否删除"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table> -->
    <el-row
      :gutter="12"
      v-for="num in Math.ceil(dataList.length / 4)"
      :key="num"
    >
      <el-col
        :span="6"
        v-for="item in dataList.slice((num - 1) * 4, num * 4)"
        :key="item.id"
      >
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size:20px"><b>{{ item.majorName + '模板'}}</b></span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="deleteHandle(item.id)"
              >删除</el-button
            >
          </div>
          <p><b>ID：</b>{{ item.id }}</p>
          <p><b>课程：</b>{{ item.courseNames }}</p>
          <p><b>年级：</b>{{ item.grade | gradeFilter }}</p>
          <p><b>专业：</b>{{ item.majorName }}</p>
          <p><b>时间：</b>{{ item.createTime }}</p>
          <el-button
            type="text"
            size="small"
            style=""
            @click="addOrUpdateHandle(item.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            style=""
            @click="playTemplate(item.id, item.grade)"
            >使用</el-button
          >
        </el-card>
      </el-col>
    </el-row>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[8, 16, 32]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="20%">
      <el-form :model="resultDta">
        <el-form-item label="模板ID" prop="templateId">
          <el-input v-model="resultDta.templateId" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="resultDta.classId" placeholder="请选择">
            <el-option
              v-for="item in classes"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitTemplate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Index from '../../../components/icon-svg/index.vue';
import AddOrUpdate from "./template-add-or-update";
export default {
  filters: {
    gradeFilter(grade) {
      switch (grade) {
        case '1':
          return "大一上";
        case '2':
          return "大一下";
        case '3':
          return "大二上";
        case '4':
          return "大二下";
        case '5':
          return "大三上";
        case '6':
          return "大三下";
        case '7':
          return "大四上";
        case '8':
          return "大四下";
      }
    },
  },
  data() {
    return {
      dataForm: {
        key: "",
      },
      resultDta: {
        templateId: '',
        grade: '',
        classId: ''
      },
      classes: [],
      dialogVisible: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 8,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
    Index
  },
  activated() {
    this.getDataList();
  },
  methods: {
    submitTemplate(){
      this.$http({
        url: this.$http.adornUrl("/edm/result/setResultByTemplate"),
        method: "post",
        params: this.$http.adornParams({
          'templateId': this.resultDta.templateId,
          'classId': this.resultDta.classId
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
                  message: '生成成功',
                  type: 'success',
                  duration: 1500
                })
        } else {
          this.$message({
                  message: '生成失败',
                  type: 'error',
                  duration: 1500
                })
        }
      this.dialogVisible = false;
      });
    },
    playTemplate(id, grade){
      this.resultDta.templateId = id;
      this.resultDta.grade = grade;
      this.$http({
        url: this.$http.adornUrl("/stu/class/listByGrade"),
        method: "get",
        params: this.$http.adornParams({
          'grade': grade
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.classes = data.classes;
        } else {
          this.classes = [];
        }
        
      this.dialogVisible = true;
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/edm/template/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/edm/template/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
